#include "unp.h"

int main(int argc, char * argv[]) {
	// if (argc < 2) {
	// 	printf("usage %s <ip address>", argv[0]);
	// 	exit(1);
	// }
	char* host = "127.0.0.1";

	SA_IN serv_addr;
	int sockfd[5]; // make 5 tcp connection to server this client

	int i;
	for (i = 0; i < 5; i++) {
		sockfd[i] = Socket(AF_INET, SOCK_STREAM, 0);
		bzero((char*)&serv_addr, sizeof(serv_addr));
		serv_addr.sin_family = AF_INET;
		serv_addr.sin_port = htons(SERV_PORT);
		Inet_pton(AF_INET, host, &(serv_addr.sin_addr));
		Connect(sockfd[i], (SA*)&serv_addr, sizeof(serv_addr));
	}
	str_cli(stdin, sockfd[0]);
	return 0;
}

void str_cli(FILE* fp, int sockfd) {
	char sendline[MAXLINE], recvline[MAXLINE];
	ssize_t n;
	// bzero(sendline, MAXLINE);
	// bzero(recvline, MAXLINE);

	while (Fgets(sendline, MAXLINE, fp) != NULL) {
		n = Write(sockfd, sendline, strlen(sendline));
		n = Read(sockfd, recvline, MAXLINE);
		Fputs(recvline, stdout);
	}
}